package com.example.administrator.newproject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;


public class YongHuTouXiangActivity extends ActionBarActivity {
    private String st;
    private File f;
    private ImageView image;
    private int count=0;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yong_hu_tou_xiang);
        Button button=(Button)findViewById(R.id.button_shoucang);
        image =(ImageView)findViewById(R.id.image__shoucang);
        image.setImageResource(R.drawable.ic_launcher);
        final SharedPreferences sha=getSharedPreferences("image.html"
                , Context.MODE_PRIVATE);
        count =sha.getInt("image",0);
        editor=sha.edit();
        Toast.makeText(YongHuTouXiangActivity.this, getExternalCacheDir().getPath(), Toast.LENGTH_SHORT).show();


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                st=count+".png";
                count++;
                f=new File(getExternalCacheDir().getPath(),st);
                editor.putInt("image",count);
                editor.commit();






                Intent i=new Intent();
                i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(i,0);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0){
            BitmapFactory.Options options=new BitmapFactory.Options();
            //为true,不会加载图片,会加载图片的宽高
            options.inJustDecodeBounds=true;

            BitmapFactory.decodeFile(f.getPath(),options);
            int h= options.outHeight;
            int w=options.outWidth;
            int width=200;
            int s=w/width;
            //缩放比例,大于一,就返回缩小的,缩放是宽高都进行缩放的
            options.inSampleSize=s;
            options.inJustDecodeBounds=false;//加载图片.二次加载,第一次获得宽高,防止内存溢出
            Bitmap bitmap= BitmapFactory.decodeFile(f.getPath(),options);
            image.setImageBitmap(bitmap);


            Intent in=new Intent();
            in.setAction("com.android.camera.action.CROP");
            in.setDataAndType(Uri.fromFile(f), "image/*");
            in.putExtra("crop",true);
            in.putExtra("aspectX",1);

            in.putExtra("aspectY",1);//宽
            in.putExtra("outputX",200);//裁剪出来的像素大小

            in.putExtra("ouyputY",200);
            in.putExtra("return-data",true);
            startActivityForResult(in,10);
            if(requestCode==10){
                Bitmap bitmap1= data.getParcelableExtra("data");//对象必须序列化
                Log.i("=============", bitmap1.toString());
                image=(ImageView)findViewById(R.id.image__shoucang);
                image.setImageBitmap(bitmap1);


            }

        }
    }



}
